<template>
  <div id="manageUserView">
    <a-table
      :ref="tableRef"
      :columns="columns"
      :data="dataList"
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
      }"
      @page-change="onPageChange"
    >
      <template #optional="{ record }">
        <a-space>
          <a-button type="primary" @click="doCheck(record)"> 查看</a-button>
          <a-button status="danger" @click="doResetPssword(record)"
            >重置密码
          </a-button>
          <a-button status="danger" @click="doDeleteuser(record)"
            >删除
          </a-button>
        </a-space>
      </template>
    </a-table>
    <a-modal
      v-model:visible="visible"
      title="用户信息"
      hideCancel="true"
      @Ok="vasible = false"
    >
      <a-form :model="formState">
        <a-form :model="formState">
          <a-form-item label="账号名">
            <a-input v-model="formState.userAccount" />
          </a-form-item>
          <a-form-item label="用户名">
            <a-input v-model="formState.userName" />
          </a-form-item>
          <a-form-item label="角色">
            <a-input v-model="formState.userRole" />
          </a-form-item>
          <a-form-item label="创建时间">
            <a-input v-model="formState.createTime" />
          </a-form-item>
        </a-form>
      </a-form>
    </a-modal>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect, reactive } from "vue";
import { User, UserControllerService, UserVO } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import * as querystring from "querystring";
import { useRouter } from "vue-router";

const tableRef = ref();
const visible = ref(false);

interface FormState {
  userAccount: string;
  userName: string;
  createTime: string;
  userRole: string;
  password: string;
}

var formState = reactive({
  userAccount: "",
  userName: "",
  createTime: "",
  userRole: "",
  password: "",
} as UserVO);

const dataList = ref([]);
const total = ref(0);
const searchParams = ref({
  pageSize: 10,
  current: 1,
});

const loadData = async () => {
  const res = await UserControllerService.listUserByPageUsingPost(
    searchParams.value
  );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("加载失败，" + res.message);
  }
};

const doDeleteuser = async (user: User) => {
  const res = await UserControllerService.deleteUserUsingPost({
    id: user.id,
  });
  if (res.code === 0) {
    message.success("删除成功");
    loadData();
  } else {
    message.error("删除失败");
  }
};

/**
 * 监听 searchParams 变量，改变时触发页面的重新加载
 */
watchEffect(() => {
  loadData();
});

/**
 * 页面加载时，请求数据
 */
onMounted(() => {
  loadData();
});

// {id: "1", title: "A+ D", content: "新的题目内容", tags: "["二叉树"]", answer: "新的答案", submitNum: 0,…}

const columns = [
  {
    title: "用户ID",
    dataIndex: "id",
  },
  {
    title: "用户账号",
    dataIndex: "userAccount",
  },
  {
    title: "用户昵称",
    dataIndex: "userName",
  },
  {
    title: "用户角色",
    dataIndex: "userRole",
  },
  {
    title: "操作",
    slotName: "optional",
  },
];

const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};

const doResetPssword = async (user: User) => {
  const res = await UserControllerService.resetPasswordUsingPost(user);
  if (res.code === 0) {
    message.success("密码已重置为ji9olJI(OL");
    loadData();
  } else {
    message.error("密码重置失败");
  }
};

const router = useRouter();

const doCheck = async (user: User) => {
  visible.value = true;
  const res = await UserControllerService.getUserVoByIdUsingGet(user.id);
  if (res.code === 0) {
    formState.createTime = res.data?.createTime;
    formState.userName = res.data?.userName;
    console.log("formState: ", formState);
    formState.userAccount = res.data?.userAccount;
    formState.userRole = res.data?.userRole;
  } else {
    message.error("用户信息获取失败，" + res.message);
  }
};
</script>

<style scoped>
#manageUserView {
}
</style>
